/**
 * 
 */
package com.jdy.haoduoaiteacher.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdy.haoduoaiteacher.intface.PhotoSourceDelegate;
import com.jdy.haoduoaiteacher.model.AlbumCollectionItem;
import com.jdy.haoduoaiteacher.util.ImageLoader;
import com.jdy.haoduoaiteacher.util.Tools;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

/**
 * @author zp 2016-12-21 上午10:44:50
 */
public class ChatWallPaperAapter extends BaseAdapter {

  private final Context mContext;

  private final ArrayList<AlbumCollectionItem> mList;

  private final SparseArray<View> saArray = new SparseArray<View>();

  private PhotoSourceDelegate mPhotoSourceDelegate;

  public ChatWallPaperAapter(Context mContext,
      ArrayList<AlbumCollectionItem> mList) {
    this.mContext = mContext;
    this.mList = mList;
  }

  public void setPhotoSourceDelegate(PhotoSourceDelegate psd) {
    this.mPhotoSourceDelegate = psd;
  }

  @Override
  public int getCount() {
    if (mList == null) {
      return 0;
    } else {
      return this.mList.size();
    }
  }

  @Override
  public Object getItem(int position) {
    if (mList == null) {
      return null;
    } else {
      return this.mList.get(position);
    }
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @SuppressLint({ "NewApi", "InflateParams" })
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    convertView = saArray.get(position);
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = LayoutInflater.from(this.mContext).inflate(
          R.layout.horizontallist_item_2, null, false);
      holder.storyimg = (ImageView) convertView.findViewById(R.id.img);
      holder.img2 = (ImageView) convertView.findViewById(R.id.img2);
      holder.storytittle = (TextView) convertView.findViewById(R.id.tittle);
      convertView.setTag(holder);
      saArray.put(position, convertView);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    holder.img2.setImageResource(R.drawable.round_image_frame);

    if (this.mList != null) {
      final AlbumCollectionItem aci = this.mList.get(position);

      if (aci.getId() == 2) {
        holder.storyimg.setImageResource(R.drawable.christmas_theme_icon);
      }

      if (aci.getId() == 3) {
        holder.storyimg.setImageResource(R.drawable.foggy_theme_icon);
      }

      if (aci.getId() == 4) {
        holder.storyimg.setImageResource(R.drawable.newspring_theme_icon);
      }

      if (aci.getId() == 5) {
        holder.storyimg
            .setImageResource(R.drawable.through_universe_theme_icon);
      }
      if (aci.getId() == 6) {
        holder.storyimg.setImageResource(R.drawable.love_theme_icon);
      }

      if (aci.getId() == 7) {
        holder.storyimg.setImageResource(R.drawable.camera_icon);
      }

      if (aci.getId() == 8) {
        holder.storyimg.setImageResource(R.drawable.gallery_icon);
      }

      holder.storyimg.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          // TODO Auto-generated method stub
          if (mPhotoSourceDelegate != null)
            mPhotoSourceDelegate.onPhotoSource(aci.getId());
        }
      });

      holder.storytittle.setText(aci.getName());

      final ImageView iiiimg = holder.storyimg;

      if (Tools.isNotEmpty(aci.getCover()))
        Picasso
            .with(mContext)
            .load(aci.getCover())
            .placeholder(
                mContext.getResources().getDrawable(R.drawable.default_cover))
            .error(
                mContext.getResources().getDrawable(R.drawable.default_cover))
            .into(holder.storyimg, new Callback() {

              @Override
              public void onSuccess() {
                // TODO Auto-generated method stub

              }

              @Override
              public void onError() {
                if (mImageLoader == null) {
                  mImageLoader = new ImageLoader(mContext);
                }
                mImageLoader.DisplayImage(aci.getCover(), iiiimg, false);

              }
            });
    }
    return convertView;
  }

  private ImageLoader mImageLoader;

  private class ViewHolder {
    ImageView storyimg, img2;
    TextView storytittle;
  }
}
